package com.course.smart_campus.controller;

import com.course.smart_campus.entity.Result;
import com.course.smart_campus.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/user")
public class userController {

    @Resource
    private UserService userService;

    /**
     * 微信登录
     * @param code
     * @return
     */
    @RequestMapping("/login")
    public Result WxLogin(@RequestParam String code) {
        Map<String, String> wxLogin = userService.WxLogin(code);
        if (wxLogin!=null){
            return new Result(true, "微信登录成功",wxLogin);
        }else{
            return new Result(false, "微信登录失败");
        }
    }

    /**
     * 昵称、图片
     */
    @RequestMapping("/upload")
    public Result upload(@RequestParam String openid, @RequestParam String nickname, @RequestParam String avatarUrl) {
        boolean flag = userService.upload(openid, nickname, avatarUrl);
        if (flag){
            return new Result(true, "昵称、图片保存成功");
        }else{
            return new Result(false, "昵称、图片保存失败");
        }
    }
    /**
     * 用户身份信息绑定
     */
    @RequestMapping("/bind")
    public Result bind(@RequestParam String openid, @RequestParam String name, @RequestParam Integer id,@RequestParam String classId) {
        boolean flag = userService.bind(openid,name,id,classId);
        if (flag){
            return new Result(true, "用户身份信息绑定成功");
        }else{
            return new Result(false, "用户身份信息绑定失败");
        }
    }

}

